import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/entities/data_base/my_database.dart';
import 'package:palmf_hid/common/utils/my_log_util.dart';
import 'package:palmf_hid/common/values/my_color.dart';
import 'package:palmf_hid/common/widgets/common_icon_button.dart';
import 'package:palmf_hid/common/widgets/empty_widget.dart';
import 'package:palmf_hid/pages/main/patient/add_pet/add_pet_view.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:syncfusion_flutter_datagrid/datagrid.dart';

import 'patient_logic.dart';
import 'patient_state.dart';

class PatientPage extends StatefulWidget {
  const PatientPage({Key? key}) : super(key: key);

  @override
  State<PatientPage> createState() => _PatientPageState();
}

class _PatientPageState extends State<PatientPage> {
  final PatientLogic logic = Get.put(PatientLogic());
  final PatientState state = Get.find<PatientLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColor.c_FFFFFFFF,
      body: Obx(() => _buildBody()),
    );
  }

  @override
  void dispose() {
    Get.delete<PatientLogic>();
    super.dispose();
  }

  _buildBody() {
    return Column(
      children: [_buildTitle(), Expanded(child: _buildList())],
    );
  }

  _buildList() {
    return Container(
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
        child: SmartRefresher(
          key: logic.refresherKey,
          enablePullDown: true,
          onRefresh: logic.onRefresh,
          controller: logic.refreshController,
          child: state.petInfoEntityList.isEmpty
              ? Center(
                  child: emptyWidget(),
                )
              : SfDataGrid(
                  columnWidthMode: ColumnWidthMode.fill,
                  source: logic.petInfoDataGridSource,
                  showCheckboxColumn: true,
                  selectionMode: SelectionMode.multiple,
                  controller: logic.buildDataGridController(),
                  columns: _obtainColumns(),
                ),
        ));
  }

  _buildItem(PetInfo petInfo, int index) {
    return Row(
      children: [
        Text(petInfo.petName),
        Text(petInfo.masterName),
        Text(petInfo.phone),
        Text(petInfo.petSex),
        Text('${petInfo.petYear}岁${petInfo.petMonth}月'),
        Text('${petInfo.petWeight}${petInfo.petWeight == "" ? "" : '(kg)'}'),
        Text(petInfo.petType),
        commonIconButtonWidget(
            onPressed: () {
              logic.handleEdit(petInfo: petInfo);
            },
            icon: Icons.edit,
            btnText: "编辑",
            color: Colors.blue)
      ],
    );
  }

  _buildTitle() {
    return Container(
        padding: const EdgeInsets.symmetric(horizontal: 15),
        height: 40,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            const Text(
              "宠物管理",
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
              ),
            ),
            commonIconButtonWidget(
                onPressed: () {
                  MyLogUtil().i("add");
                  logic.handleAdd();
                },
                btnText: "添加",
                icon: Icons.add,
                color: Colors.green),
          ],
        ));
  }

  List<GridColumn> _obtainColumns() {
    return <GridColumn>[
      GridColumn(
        width: 120.0,
        columnName: 'id',
        label: Container(
          padding: const EdgeInsets.all(8),
          alignment: Alignment.centerRight,
          child: const Text(
            'Order ID',
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
      GridColumn(
        width: 150.0,
        columnName: 'customerId',
        label: Container(
          padding: const EdgeInsets.all(8),
          alignment: Alignment.centerRight,
          child: const Text(
            'Customer ID',
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
      GridColumn(
        width: 120.0,
        columnName: 'name',
        label: Container(
          padding: const EdgeInsets.all(8),
          alignment: Alignment.centerLeft,
          child: const Text(
            'Name',
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
      GridColumn(
        width: 110.0,
        columnName: 'freight',
        label: Container(
          padding: const EdgeInsets.all(8),
          alignment: Alignment.centerRight,
          child: const Text(
            'Freight',
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
      GridColumn(
        width: 120.0,
        columnName: 'city',
        label: Container(
          padding: const EdgeInsets.all(8),
          alignment: Alignment.centerLeft,
          child: const Text(
            'City',
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
      GridColumn(
          width: 120.0,
          columnName: 'price',
          label: Container(
            padding: const EdgeInsets.all(8),
            alignment: Alignment.centerRight,
            child: const Text(
              'Price',
              overflow: TextOverflow.ellipsis,
            ),
          ))
    ];
  }
}
